package cn.dongqihong.oauth2.service;

import cn.dongqihong.oauth2.entity.App;
import cn.dongqihong.oauth2.entity.AppToken;
import cn.dongqihong.oauth2.mapper.AppMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Service
public class AppService {
    @Resource
    private AppMapper appMapper;

    public void createApp(App app) {
        appMapper.insertInfo(app);
    }

    public App findById(Long id) {
        return appMapper.findById(id);
    }

    /**
     * 通过code获取Token
     *
     * @param code
     * @return
     */
    public AppToken findTokenByCode(Long code) {
        return appMapper.findTokenByCode(code);
    }

    /**
     * 查看Token
     *
     * @param accessToken
     * @return AppToken
     */
    public AppToken findTokenByAccessToken(String accessToken) {
        return appMapper.findTokenByAccessToken(accessToken);
    }

    /**
     * 通过appId和userId查询Token
     *
     * @param appId
     * @param userId
     * @return
     */
    public AppToken findTokenByAppIdAndUserId(Long appId, Long userId) {
        return appMapper.findTokenByAppIdAndUserId(appId, userId);
    }

    /**
     * 刷新token过期时间
     *
     * @param refresh_token
     * @return 新的token
     */
    public AppToken refreshToken(String refresh_token) {
        AppToken appToken = appMapper.findTokenByRefreshToken(refresh_token);
        Long nowMillis = System.currentTimeMillis();
        Long validTillMillis = nowMillis + 7 * 24 * 60 * 60 * 1000;//一周
        Map<String, Object> appTokenNew = new HashMap<>();
        Date validTill = new Date(validTillMillis);
        appToken.setValidTill(validTill);
        appTokenNew.put("code", appToken.getCode());
        appTokenNew.put("validTill", validTill);
        appMapper.updateRefreshTokenByCode(appTokenNew);
        return appToken;
    }

    /**
     * 创建Token
     *
     * @param appId  应用ID
     * @param userId 用户ID
     * @return code code
     */
    public Long createToken(Long appId, Long userId) {
        AppToken oldToken = findTokenByAppIdAndUserId(appId, userId);
        if (oldToken != null) {
            refreshToken(oldToken.getRefreshToken());
            return oldToken.getCode();
        } else {
            Long nowMillis = System.currentTimeMillis();
            Long validTillMillis = nowMillis + 7 * 24 * 60 * 60 * 1000;//一周
            String accessToken = UUID.randomUUID().toString().replace("-", "");
            String refreshToken = UUID.randomUUID().toString().replace("-", "");
            Map<String, Object> token = new HashMap<>();
            token.put("appId", appId);
            token.put("userId", userId);
            token.put("accessToken", accessToken);
            token.put("refreshToken", refreshToken);
            token.put("addTime", new Date(nowMillis));
            token.put("validTill", new Date(validTillMillis));
            Long code = appMapper.insertToken(token);
            return code;
        }
    }
}
